package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.loan.Calendar;
import com.ucredit.babel.common.model.loan.CalendarInfo;
import com.ucredit.babel.common.model.loan.CalendarInfoRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 三方日历
 *
 * @author guoqiang
 */

public class CalendarInfoRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 三方日历
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        CalendarInfoRecord obj = (CalendarInfoRecord) model;
        CalendarInfo data = obj.getCalendarInfo() == null ? new CalendarInfo() : obj.getCalendarInfo();
        StringBuilder sb = getBaseInfo(obj, successful);
        setFileNum(5);
        StringBuilder TxSb = new StringBuilder(500);
        List<Calendar> calendars = data.getCalendars();
        for (Calendar c : calendars) {
            TxSb.delete(0, TxSb.length());
            //姓名
            TxSb.append(data.getName() == null ? "" : data.getName()).append(CRT001);
            //手机号
            TxSb.append(data.getMobile() == null ? "" : data.getMobile()).append(CRT001);
            //身份证号
            TxSb.append(data.getIdno() == null ? "" : data.getIdno()).append(CRT001);
            //标题
            TxSb.append(c.getTitle() == null ? "" : c.getTitle()).append(CRT001);
            //内容
            TxSb.append(c.getContent() == null ? "" : c.getContent()).append(CRT001);
            //开始时间
            TxSb.append(c.getStartTime() == null ? "" : c.getStartTime()).append(CRT001);
            //结束时间
            TxSb.append(c.getEndTime() == null ? "" : c.getEndTime()).append(CRT001);
            //修改时间
            TxSb.append(c.getLastModifyTime() == null ? "" : c.getLastModifyTime()).append(CRT001);
            //时区
            TxSb.append(c.getTimezone() == null ? "" : c.getTimezone()).append(CRT001);
            //持续时间
            TxSb.append(c.getDuration() == null ? "" : c.getDuration()).append(CRT001);
            //组织人
            TxSb.append(c.getOrganiser() == null ? "" : c.getOrganiser()).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_calendar_info_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.CALENDAR_INFO.toString();
    }

    @Override
    public String getComment() {
        return "三方日历";
    }
}
